/*****************************************************************************
 *
 * MODULE NAME:    sys_main.c
 * PROJECT CODE:   BlueStream
 * DESCRIPTION:    entry point to the Bluetooth stack
 * MAINTAINER:     Ivan Griffin
 * DATE:           21 January 2000
 *
 * SOURCE CONTROL: $Id: sys_main.c,v 1.19 2009/08/14 05:42:33 xiezch Exp $
 *
 * LICENSE:
 *     This source code is copyright (c) 2000-2004 Ceva Inc.
 *     All rights reserved.
 *
 * REVISION HISTORY:
 *    21 Jan 2000  -   IG       - Initial Version
 *    15 Jun 2000  -   IG       - Updated for sys_post module
 *
 * SOURCE:
 * ISSUES:
 *
 *****************************************************************************/

#include "sys_config.h"
#include "sys_types.h"
#include "sys_const.h"
#include "sys_post.h"
#include "sys_rda_arm.h"

#include "bt_init.h"
#include "bt_mini_sched.h"
#include "lmp_config.h"

#include "sys_irq.h"

#include "hw_lc.h"
#include "hw_leds.h"
#include "hw_register.h"
#include "hw_hab_defs.h"

#include "tra_hcit.h"

#include "tra_combined.h"
#include "tra_uart.h"

extern struct tra_hcit_uart_info sys_hcit_uart_info;

extern struct tra_hcit_combined_info sys_hcit_combined_info;
extern void *TRAhcit_Register_COMBINED(void);

extern t_SYS_Config g_sys_config;

extern void SYShal_config_Update(void);


// patch
extern void p_hid_init(void);
extern u_int8 p_hid_Schedule(void);

/*
 * Local Functions
 */
static void _SYS_Main_Loop(void);

/*****************************************************************************
 *
 *
 *****************************************************************************/

#if ((BUILD_TYPE!=FLASH_BUILD) || (defined(ADS_TOOLCHAIN_SUPPORTED)))

#if (BUILD_TYPE==FLASH_BUILD)
void SYS_Vector_Init_Block(void);
#endif

void SYS_Main(void);

int main (void)
{
    SYS_Main(); /* never returns */

#if (BUILD_TYPE==FLASH_BUILD)
    SYS_Vector_Init_Block();
#endif
    return 0;
}
#endif

void SYS_Main(void)
{
    /*
     * WARNING: Following code is duplicated in SYS_HC_INit()
     * ARM ADS COMPILER BUG DOES NOT ALLOW FUNCTION CALL AT THIS POINT
     * */

    u_int32 flags;

    SYSirq_Disable_Interrupts_Save_Flags(&flags);

    SYSconfig_Initialise();

    /*
     * Set up the HCI transport registration function (this function
     * is called on intialisation or re-initialisation of the HCI
     * transport sub-system
     */
#if TRA_HCIT_UART_SUPPORTED==1
    if(g_sys_config.hci_type == TRA_HCIT_TYPE_UART ||
        g_sys_config.hci_type == TRA_HCIT_TYPE_BCSP)
        TRAhcit_Set_Register_Function(TRAhcit_Register_UART_Driver);
#endif
#if (TRA_HCIT_USB_SUPPORTED == 1)
    if(g_sys_config.hci_type == TRA_HCIT_TYPE_USB)
        TRAhcit_Set_Register_Function(TRAhcit_Register_USB_Driver);
#endif
#if (TRA_HCIT_COMBINED_SUPPORTED == 1)
    if(g_sys_config.hci_type == TRA_HCIT_TYPE_COMBINED)
        TRAhcit_Set_Register_Function(TRAhcit_Register_COMBINED);
#endif
#if (TRA_HCIT_SPI_SUPPORTED == 1)
    if(g_sys_config.hci_type == TRA_HCIT_TYPE_SPI)
        TRAhcit_Set_Register_Function(TRAhcit_Register_SPI_Driver);
#endif

    BT_Initialise();

#if (TRA_HCIT_COMBINED_SUPPORTED == 1)
    {
        extern volatile int prh_combined_hci_ready;
        prh_combined_hci_ready=1;
    }
#endif
    SYSirq_Enable_Interrupts_Save_Flags(&flags);

	p_hid_init();
	//p_hid_isr();
	//p_hid_Schedule();
	p_hid_Run_Spec_Fun(NULL,0);

    _SYS_Main_Loop();
}

static void _SYS_Main_Loop(void)
{
    while(1)
    {
        BTms_Sched(0x1);
#if (BUILD_EMULATION != 1)
        SYShal_config_Update();
#endif
    }
}

